<template>
  <div class="register">
    <el-form
      ref="registerForm"
      :model="registerForm"
      :rules="registerRules"
      label-position="left"
      class="register-form"
      auto-complete="on"
    >
      <h3 class="title">注册</h3>
      <el-form-item prop="account" label="用户名">
        <el-input
          v-model="registerForm.account"
          placeholder="请输入用户名"
          name="account"
          type="text"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password" label="密码">
        <el-input
          v-model="registerForm.password"
          placeholder="请输入密码"
          name="password"
          type="password"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="confirmPassword" label="确认密码">
        <el-input
          v-model="registerForm.confirmPassword"
          placeholder="请确认密码"
          name="confirmPassword"
          type="password"
          auto-complete="on"
        />
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.prevent="handleRegister"
        >注册</el-button
      >

      <div class="login-link">
        <span @click="goToLogin">已有账号？去登录</span>
      </div>
    </el-form>
  </div>
</template>

<script>
import { register } from '@/api/base'; 
import { Message } from 'element-ui';

export default {
  name: "RegisterView",
  data() {
    // 用户名校验规则
    const validateUsername = (rule, value, callback) => {
      if (!value || value.trim().length < 6 || value.trim().length > 16) {
        callback(new Error("用户名长度必须在6到16位之间，且不能包含空格"));
      } else {
        callback();
      }
    };

    // 密码校验规则
    const validatePassword = (rule, value, callback) => {
      if (!value || value.trim().length < 6 || value.trim().length > 16) {
        callback(new Error("密码长度必须在6到16位之间，且不能包含空格"));
      } else {
        callback();
      }
    };

    // 确认密码校验规则
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };

    return {
      registerForm: {
        account: "",
        password: "",
        confirmPassword: "",
      },
      registerRules: {
        account: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        confirmPassword: [
          { required: true, trigger: "blur", validator: validateConfirmPassword },
        ],
      },
      loading: false,
    };
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then((result) => {
            this.loading = false;
            if (result.data.code === 200) {
              Message.success("注册成功");
              this.$router.push('/'); // 注册成功后跳转到登录页面
            } else {
              Message.error(result.data.msg);
            }
          }).catch((error) => {
            this.loading = false;
            Message.error("注册失败，请稍后再试");
            console.error(error);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    goToLogin() {
      this.$router.push('/'); // 跳转到登录页面
    },
  },
};
</script>

<style lang='scss' scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url('../assets/login-background.png'); 
  background-size: cover;
}

.register-form {
  width: 360px;
  padding: 40px 35px 20px 35px;
  background: white;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 16px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.15);
  .el-input {
    input {
      border-radius: 16px; // 更大的圆角
      border: 1px solid #dcdfe6;
      font-size: 16px;
      padding: 10px 20px;
      height: 40px;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1) inset;
      transition: border-color 0.3s, box-shadow 0.3s;
      &:focus {
        border-color: #409eff;
        box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
      }
    }
  }
  .el-form-item {
    margin-bottom: 10px;
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-link {
  text-align: center;
  margin-top: 1px;
  font-size: 14px;
  color: #007aff;
  cursor: pointer;
  transition: color 0.3s;
  &:hover {
    color: #0056b3;
  }
}
.el-button {
  height: 40px;
  width: 100%;
  border-radius: 14px;
  background-color: #007aff;
  border-color: #007aff;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  transition: background-color 0.3s, border-color 0.3s;
  &:hover {
    background-color: #0056b3;
    border-color: #0056b3;
  }
}
</style>